<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="${host_url}/js/app/jquery.min.js"></script>
<script src="/js/app/dropload.min.js"></script>
<script src="${host_url}/js/app/tip_pop.js"></script>
<link rel="stylesheet" href="${host_url}/css/app/common.css">
<link rel="stylesheet" href="/css/app/zengsong.css">
<link rel="stylesheet"  href="${host_url}/css/app/invite_friend.css">
<link rel="stylesheet"  href="${host_url}/css/app/dropload.css">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<title>赠送折扣劵体验</title>
<script type="text/javascript">
$(function(){
	/*  页数 */
	var page_index_all = 2;/*  全部 */
    /* dropload 加载分页 */
    <c:if test="${childList.size()!=0}">
    	loadPagination("listAll",page_index_all,"logAll");
	</c:if>
});
function loadPagination(div_id, page_index, ul_id){
	$('#'+div_id).dropload({
        scrollArea : window,
        loadDownFn : function(me){
            $.ajax({
                type: 'get',
                url: 'ticketGiveMoreItem?page_index='+page_index+'&level=${level}',
                dataType: 'text',
                beforeSend: function(request) {
    				request.setRequestHeader("AUTHORIZATION","${token}");
    				request.setRequestHeader("SECRET","${memberId}");
    				request.setRequestHeader("OS","${os}");
    			 }, 
                contentType:"application/x-www-form-urlencoded; charset=UTF-8",
                success: function(msg){
                    if($.trim(msg)){
                		page_index++;
                    	$("#"+ul_id).append(msg);
                    	me.resetload();
                   	/*  如果没有数据 */
                    } else{
                        	/*  锁定 */
                        me.lock();
                        	/*  无数据 */
                        me.noData();
	                    	/*  $("#"+div_id+" .dropload-load").parent().remove(); */
	                    	  $("#"+div_id+" .dropload-load").html("没有更多啦~");
                    } 
                },
	                error: function(xhr, type){
                    	/*  即使加载出错，也得重置 */
                    me.resetload();
                } 
            });
        }
    });
}
function fn_check_radioOne(s_id,p_id,id){
	 if($("#logAll").children().length==1){
		 $("#logAll").children().each(function(){
			 $(this).children().siblings().each(function(){
				if( $(this).attr('class')=='rightarea'){
					if($(this).children().attr('class')=='btn-check check'){
						if($(this).parent().attr('value')==id){
							$(this).children().removeClass('check');
							$('#remove_id').val(id);
							$('#'+id).remove();
						}
			 		}
				}
			 }); 
		 }); 
	 }else{
		 $("#logAll").children().siblings().each(function(){
			 $(this).children().siblings().each(function(){
				if( $(this).attr('class')=='rightarea'){
					if($(this).children().attr('class')=='btn-check check'){
						if($(this).parent().attr('value')==id){
							$(this).children().removeClass('check');
							$('#remove_id').val(id);
							$('#'+id).remove();
						}
			 		}
				}
			 }); 
		 }); 
	 }
	 if($('#remove_id').val()!=id){
		 $("#"+s_id).addClass('check');
		 $('#all_id').append('<input type="hidden" name="level" id="'+id+'" value="'+id+'" />')
	 }
	 $('#remove_id').val(0);
	 $('#all_r').removeClass('check');
	 $('#type_s').val(0);
	 var n=0;
	 if($("#logAll").children().length==1){
		 $("#logAll").children().each(function(){
			 $(this).children().siblings().each(function(){
				if( $(this).attr('class')=='rightarea'){
					if($(this).children().attr('class')=='btn-check'){
						n=n+1;
					}
				}
			 }); 
		 });
	 }else{
		 $("#logAll").children().siblings().each(function(){
			 $(this).children().siblings().each(function(){
				if( $(this).attr('class')=='rightarea'){
					if($(this).children().attr('class')=='btn-check'){
						n=n+1;
					}
				}
			 }); 
		 });
	 }
	 if(n==0){
		 $('#all_r').addClass('check');
	 }
}
function fn_check_all(type){
	if($('#type_s').val() !=type){
		$('#all_r').addClass('check');
		if($("#logAll").children().length==1){
			 $("#logAll").children().each(function(){
				 $(this).children().siblings().each(function(){
						if( $(this).attr('class')=='rightarea'){
							if($(this).children().attr('class')=='btn-check'){
								$(this).children().addClass('check');
								var id=$(this).attr('value');
								 $('#all_id').append('<input type="hidden" name="level" id="'+ $(this).parent().attr('value')+'" value="'+$(this).parent().attr('value')+'" />')
							}
						}
				}); 
			}); 
		}else{
			 $("#logAll").children().siblings().each(function(){
				 $(this).children().siblings().each(function(){
						if( $(this).attr('class')=='rightarea'){
							if($(this).children().attr('class')=='btn-check'){
								$(this).children().addClass('check');
								var id=$(this).attr('value');
								 $('#all_id').append('<input type="hidden" name="level" id="'+ $(this).parent().attr('value')+'" value="'+$(this).parent().attr('value')+'" />')
							}
						}
				}); 
			}); 
		}
	
		 $('#type_s').val(1);
	}else{
		$('#all_r').removeClass('check');
		 if($("#all_id").children().length !=0){
			 $("#all_id").children().each(function(){
				 $(this).remove();
			 }); 
		 }
		 if($("#logAll").children().length==1){
			 $("#logAll").children().each(function(){
				 $(this).children().siblings().each(function(){
					if( $(this).attr('class')=='rightarea'){
						if($(this).children().attr('class')=='btn-check check'){
							$(this).children().removeClass('check');
						}
					}
				 }); 
			 });
		 }else{
			 $("#logAll").children().siblings().each(function(){
				 $(this).children().siblings().each(function(){
					if( $(this).attr('class')=='rightarea'){
						if($(this).children().attr('class')=='btn-check check'){
							$(this).children().removeClass('check');
						}
					}
				 }); 
			 });
		 }
		 $('#type_s').val(0);
	}
	
}
function fn_submit_more(){
	var child_id='';
	var fields = $("#adfrm").serializeArray();
	if(fields.length==0){
		tip("请选择要赠送的好友",250);
		return false;
	}
	$.each(fields, function(i, field){
		if(i==$("#adfrm").serializeArray().length-1){
			child_id=child_id+field.value
		}else{
			child_id=child_id+field.value+','
		}
			
		
	});
	child_id = child_id.replace(/\s+/g,"");       
	$.ajax({
		type:"POST", 	
		url:'updateChildLevel?child_member_id='+child_id+'&level=${level}',	 		

		beforeSend: function(request) {
			request.setRequestHeader("AUTHORIZATION","${token}");
			request.setRequestHeader("SECRET","${memberId}");
			request.setRequestHeader("OS","${os}");
		 }, 
		success: function (data) {

			if(data.state=="16003"){
				tip(data.msg,250);
			} 
			if(data.state=="16004"){
				tip(data.msg,250);
			} 
			if(data.state=="16001"){
				tip(data.msg,250);
				setTimeout(function(){
					if('${os}'=='ios'){
						window.webkit.messageHandlers.closeView.postMessage('');
					}
					if('${os}'=='android'){
						android.finishUrl()
					} 

				},1000);
				
			}
		 },	 
		
	}); 
	
}
function closeView() {}
</script>
</head>
<body>
<input type="hidden" id="remove_id" value="" />
<input type="hidden" id="type_s" value="0" />
<form  method="post" id="adfrm">
<div id="all_id"></div>
</form>
<div class="list" >

	<h4 class=" clearfix">
		<p class="leftArea">可赠送<span>${rate }折</span>体验券的好友</p>
		<p class="rightArea">全选<span class="btn-check" id="all_r"><input type="radio" onclick="fn_check_all('1')"></span></p>
	</h4>
	<c:if test="${childList.size()!=0}">
	<div id="listAll">
	 <ul id="logAll">
		<c:forEach items="${childList}" var="c">
			<div class="flex-layout" id="p_${c.id}" value="${c.id}">
		       <p class="leftarea"><img src="${c.logo}" alt=""></p>
		       <p class="centerarea"><span>${c.nick_name}</span><span>${c.tel_h}</span></p>
		       <p class="rightarea"><span class="btn-check" id="s_${c.id}"><input type="radio" onclick="fn_check_radioOne('s_${c.id}','p_${c.id}','${c.id}')"></span></p>
		    </div>
	    </c:forEach>
	  </ul>
	</div>
    <button class="cs_next" onclick="fn_submit_more()">确定</button>
	</c:if> 
	<c:if test="${childList.size()==0}">
		<div class="zanwumore">
			  <span><img src="/img/app/zanwu.png"></span>
			  <p>暂无可赠送的会员</p>
		</div>
	</c:if> 
 </div>
</body>
</html>